package com.e3roid.examples;

import android.widget.Toast;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Sprite;
import com.e3roid.drawable.texture.Texture;
import com.e3roid.drawable.texture.AssetTexture;
import com.e3roid.event.SceneUpdateListener;

public class SceneUpdateExample extends E3Activity implements SceneUpdateListener {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private Sprite  robot;
	private Texture robotTexture;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		
		// invoke onUpdateScene on every 3 second.
		scene.registerUpdateListener(3000, this);
		
		int centerX = (getWidth()  - robotTexture.getWidth())  / 2;
		int centerY = (getHeight() - robotTexture.getHeight()) / 2;
		
		robot = new Sprite(robotTexture, centerX, centerY);
		scene.getTopLayer().add(robot);
		return scene;
	}

	@Override
	public void onLoadResources() {
		robotTexture = new AssetTexture("robot.png", this);
	}
	
	/*
	 * Post GUI events to ui thread because onUpdateScene method is called from another thread.
	 */
	@Override
	public void onUpdateScene(E3Scene scene, final long elapsedMsec) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(getContext(), String.format("onUpdateScene elapsed=%d msec", elapsedMsec), Toast.LENGTH_SHORT).show();
			}
		});
	}
}